package org.bill.util;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.bill.entity.BillDetail;

import com.miu.time.TimeU;

public class GeneralUtil {

    // *******************************************************
    // -------------------------------------------------------
    // @constant

    public static int DEFAULT_NAME = 11111;

    public static String DEFAULT_NOTE = "暂无";
    
    public static String DEFAULT_STR = "未知";

    // *******************************************************
    // -------------------------------------------------------
    // @variable

    /**
     * 随机后缀
     */
    private static int _suffix = 0;

    // *******************************************************
    // -------------------------------------------------------
    // @methods

    /**
     * 生成账单id
     * 
     * @return 11位字符串
     */
    public static synchronized String getBillId() {
        _suffix = 0;

        String result = TimeU.formatDateTime(new Date(), "yyyyMMddHHmmsss");
        _suffix++;
        if (_suffix > 999) {
            _suffix = 0;
        }
        result = result + String.format("%03d", _suffix);

        return result;
    }

    /**
     * 生成用户id
     * 
     * @return 6位数字
     */
    public static synchronized int getUserId() {
        int result = 999999;

        Random ran = new Random();
        result = ran.nextInt(999999);
        while (result < 100000) {
            result = ran.nextInt(999999);
        }

        return result;
    }

    /**
     * 计算账单总额
     * 
     * @param details
     *            账单明细
     * @return 账单总额
     */
    public static double getTotalPrice(List<BillDetail> details) {
        double result = 0;
        if (details == null || details.isEmpty()) {
            return result;
        }

        for (BillDetail d : details) {
            result += d.getBilldPrice();
        }

        return result;
    }

}
